//this decides if we pull from the DB or the Cache
//then it sends data to the BrowseSpecificGUI 
package edu.unc.comp410.colemangeer.data;

import edu.unc.comp410.colemangeer.model.*;
import edu.unc.comp410.colemangeer.ui.*;

import java.io.FileNotFoundException;
import java.util.*;

public class DataChooser {
	
	private static final String machine = "customer1";
	private static final String DB = "classroom";
	
	public static void searchAll(){
		//if the cache is full, then grab from it
		//otherwise, grab from the DB
		if( DB_Cache.isFull() ){
			//send cache to BrowseSpecificGUI
			if( BrowseGeneralGUI.USE_EMULATOR ){ //Off the network, so use the Emulator
				BrowseSpecificGUI bsGUI = new BrowseSpecificGUI( DB_Cache.getFullCacheSet_Test() ); //test method call - this fork only required if "return true" is hardcoded
			}
			else{ //USE_EMULATOR is false, so we must be on the UNC network, use DB
				BrowseSpecificGUI bsGUI = new BrowseSpecificGUI( DB_Cache.getCacheSet() );
			}
		}
		else{ //the cache is not full, so grab from DB
			Set<Product> getAllSet;
			if( BrowseGeneralGUI.USE_EMULATOR ){ //Off the network, so use the Emulator
				try{
					ProductGatewayEmulator PG = new ProductGatewayEmulator(); //also, make sure to populate the DB_Cache.TableSize var - do this by calling productgateway.gettablesize and setting the db_cache.settablesize
					//DB_Cache.setTableSize( PG.getTableSize() );
					getAllSet = PG.getAllProducts();  //run the "getAll" query
					DB_Cache dbc = new DB_Cache( getAllSet ); //fill the cache
					BrowseSpecificGUI bsGUI = new BrowseSpecificGUI(getAllSet);
					//System.out.println("set passed to spinner is: *" + getAllSet.toString() + "*");
				}
				catch (FileNotFoundException x) {
					x.printStackTrace();
				}
			}
			else{ //USE_EMULATOR is false, so we must be on the UNC network, use DB
				/*try{*/
					ProductGateway PG = new ProductGateway(DB, machine); //also, make sure to populate the DB_Cache.TableSize var - do this by calling productgateway.gettablesize and setting the db_cache.settablesize
					DB_Cache.setTableSize( PG.getTableSize() );
					getAllSet = PG.getAllProducts();  //run the "getAll" query
					DB_Cache dbc = new DB_Cache( getAllSet ); //fill the cache
					BrowseSpecificGUI bsGUI = new BrowseSpecificGUI(getAllSet);
					//System.out.println("set passed to spinner is: *" + getAllSet.toString() + "*");
				/*}
				catch (FileNotFoundException x) {
					x.printStackTrace();
				}*/
				
				
			} //end of if USE_EMULATOR
		} //end of if DB_Cache isFull
	} //end of searchAll
	
	
	public static void searchByName( String theSearchString ) throws ClassNotFoundException {
		//if the cache is full, then grab it from the cache
		//if not, then grab from DB (maybe always grab from DB)
		
		boolean result = false;		
		
		if( DB_Cache.isFull() ){ //then grab from the cache (or the DB if you want to cheat)
			//*red* this currently cheats by copying the code from the DB-searchByName section
			Set<Product> getByNameSet;	
			if( BrowseGeneralGUI.USE_EMULATOR ){ //Off the network, so use the Emulator
					try{
						ProductGatewayEmulator PG = new ProductGatewayEmulator(); //also, make sure to populate the DB_Cache.TableSize var - do this by calling productgateway.gettablesize and setting the db_cache.settablesize
						//DB_Cache.setTableSize( PG.getTableSize() );
						getByNameSet = PG.getByName( theSearchString );  //run the "byName" query
						DB_Cache dbc = new DB_Cache( getByNameSet ); //fill the cache
						BrowseSpecificGUI bsGUI = new BrowseSpecificGUI(getByNameSet);
						//System.out.println("set passed to spinner is: *" + getByNameSet.toString() + "*");
					}
					catch (FileNotFoundException x) {
						x.printStackTrace();
					}
				}
				else{ //USE_EMULATOR is false, so use the real DB
					ProductGateway PG = new ProductGateway( DB, machine); //also, make sure to populate the DB_Cache.TableSize var - do this by calling productgateway.gettablesize and setting the db_cache.settablesize
					DB_Cache.setTableSize( PG.getTableSize() );
					getByNameSet = PG.getByName( theSearchString );  //run the "byName" query
					DB_Cache dbc = new DB_Cache( getByNameSet ); //fill the cache
					BrowseSpecificGUI bsGUI = new BrowseSpecificGUI(getByNameSet);
					
				} //end else of EMULATOR
		}
		else{ //grab from the DB
			//*red* this currently cheats by copying the code from the DB-searchByName section
			Set<Product> getByNameSet;	
			if( BrowseGeneralGUI.USE_EMULATOR ){ //Off the network, so use the Emulator
					try{
						ProductGatewayEmulator PG = new ProductGatewayEmulator(); //also, make sure to populate the DB_Cache.TableSize var - do this by calling productgateway.gettablesize and setting the db_cache.settablesize
						//DB_Cache.setTableSize( PG.getTableSize() );
						getByNameSet = PG.getByName( theSearchString );  //run the "byName" query
						DB_Cache dbc = new DB_Cache( getByNameSet ); //fill the cache
						BrowseSpecificGUI bsGUI = new BrowseSpecificGUI(getByNameSet);
						//System.out.println("set passed to spinner is: *" + getByNameSet.toString() + "*");
					}
					catch (FileNotFoundException x) {
						x.printStackTrace();
					}
				}
				else{ //USE_EMULATOR is false, so use the real DB
					ProductGateway PG = new ProductGateway( DB, machine); //also, make sure to populate the DB_Cache.TableSize var - do this by calling productgateway.gettablesize and setting the db_cache.settablesize
					DB_Cache.setTableSize( PG.getTableSize() );
					getByNameSet = PG.getByName( theSearchString );  //run the "byName" query
					DB_Cache dbc = new DB_Cache( getByNameSet ); //fill the cache
					BrowseSpecificGUI bsGUI = new BrowseSpecificGUI(getByNameSet);
					
				} //end else of EMULATOR
		} //end else grab from the DB
	} //end searchByName
	
	
}
